﻿'
' Will Strohl (wills@strohlsitedesign.com)
' http://www.willstrohl.com
'
'Copyright (c) 2009, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of StrohlSiteDesign.com nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections.Generic

Namespace WillStrohl.Modules.FeedHandler

    ''' <summary>
    ''' ViewRules - this class is a UI to manage feed rules
    ''' </summary>
    ''' <remarks></remarks>
    ''' <history>
    ''' </history>
    Partial Public Class ViewRules
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private p_FeedId As Integer = Null.NullInteger

#End Region

#Region " Private Properties "

        ''' <summary>
        ''' FeedId - this is the ID of the feed whose rules we are managing
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private ReadOnly Property FeedId() As Integer
            Get
                If Me.p_FeedId <> Null.NullInteger Then
                    Return Me.p_FeedId
                Else
                    Dim objFeed As Object = Request.Params("FeedId")
                    If Not objFeed Is Nothing Then
                        Me.p_FeedId = Integer.Parse(objFeed.ToString, Globalization.NumberStyles.Integer)
                        Return Me.p_FeedId
                    End If
                End If

                Return Me.p_FeedId
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Not Page.IsPostBack Then
                    Me.BindData()
                End If
            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub lnkReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkReturn.Click
            Response.Redirect(NavigateURL)
        End Sub

        Private Sub lnkAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddNew.Click
            Response.Redirect(EditUrl(Null.NullString, Null.NullString, "EditRule", String.Concat("FeedId=", Me.FeedId.ToString)))
        End Sub

        Private Sub dlRules_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlRules.ItemCommand
            Dim intArg As Integer = Integer.Parse(e.CommandArgument.ToString, Globalization.NumberStyles.Integer)
            Select Case e.CommandName
                Case "EditRule"
                    Dim objRule As New FeedRuleInfo
                    objRule = FeedHandlerController.GetFeedRule(intArg)
                    If Not objRule Is Nothing Then
                        Response.Redirect(EditUrl(Null.NullString, Null.NullString, "EditRule", String.Concat("FeedId=", objRule.FeedId, "&RuleId=", objRule.RuleId)))
                    Else
                        DotNetNuke.UI.Skins.Skin.AddModuleMessage(Me, Me.GetLocalizedString("Error.RuleIdNull"), Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
                    End If
                Case "DeleteRule"
                    FeedHandlerController.DeleteFeedRule(intArg)
                    Me.BindData()
            End Select
        End Sub

        Private Sub dlRules_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlRules.ItemDataBound
            Dim obj As Object = e.Item.FindControl("imgDelete")
            If Not obj Is Nothing Then
                Dim img As New ImageButton
                img = CType(obj, ImageButton)
                img.Attributes.Add("onclick", String.Format("return confirm('{0}');", Me.GetLocalizedString("imgDelete.Confirm")))
            End If
        End Sub

#End Region

#Region " Private Methods "

        ''' <summary>
        ''' BindData - this method initializes the binding of all of the controls
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub BindData()
            Me.LocalizePage()

            Me.ToggleView(False)

            Dim coll As New FeedRuleInfoCollection
            coll = FeedHandlerController.GetFeedRules(Me.FeedId)

            If Not coll Is Nothing Then
                If coll.Count > 0 Then
                    Me.dlRules.DataSource = coll
                    Me.dlRules.DataBind()
                    ToggleView(True)
                End If
            End If
        End Sub

        ''' <summary>
        ''' LocalizePage - this method centralizes the localization of the controls
        ''' </summary>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub LocalizePage()
            Me.lnkReturn.Text = Me.GetLocalizedString("lnkReturn.Text")
            Me.lnkAddNew.Text = Me.GetLocalizedString("lnkAddNew.Text")
            Me.lblNoRecords.Text = Me.GetLocalizedString("lblNoRecords.Text")
        End Sub

        ''' <summary>
        ''' ToggleView - this method toggles the view of the user control
        ''' </summary>
        ''' <param name="ShowList"></param>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Private Sub ToggleView(ByVal ShowList As Boolean)
            Me.dlRules.Visible = ShowList
            Me.lblNoRecords.Visible = Not ShowList
        End Sub

#End Region

#Region " Protected Methods "

        ''' <summary>
        ''' TrimToDisplay - this method facilitates the need to conditionally trim expressions in the UI
        ''' </summary>
        ''' <param name="obj"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Protected Function TrimToDisplay(ByVal obj As Object) As String
            If Not obj Is Nothing Then
                Dim strText As String = obj.ToString
                If Not String.IsNullOrEmpty(strText) Then
                    If strText.Length > 50 Then
                        Return Left(String.Concat(Server.HtmlEncode(strText), " ..."), 46)
                    Else
                        Return Server.HtmlEncode(strText)
                    End If
                Else
                    Return String.Empty
                End If
            Else
                Return String.Empty
            End If
        End Function

#End Region

    End Class

End Namespace